package com.dlwx.parttimetalent.fragment;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.DaRenAdapter;
import com.dlwx.parttimetalent.adapter.HomeClassifyAdapter;
import com.dlwx.parttimetalent.adapter.NearByImageAdapter;
import com.dlwx.parttimetalent.bean.BannerBean;
import com.dlwx.parttimetalent.bean.DaRenBean;
import com.dlwx.parttimetalent.bean.LabelClassifyBean;
import com.dlwx.parttimetalent.bean.NearByInfoBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.OnRecyclerViewItemClickListener;
import com.dlwx.parttimetalent.ui.AllClassifyActivity;
import com.dlwx.parttimetalent.ui.AttentionUserClassifyActivity;
import com.dlwx.parttimetalent.ui.CityListActivity;
import com.dlwx.parttimetalent.ui.ClassifyListActivity;
import com.dlwx.parttimetalent.ui.NearByActivity;
import com.dlwx.parttimetalent.ui.SalesmanActivity;
import com.dlwx.parttimetalent.ui.SearchActivity;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.DisplayUtil;
import com.dlwx.parttimetalent.utils.GlideImageLoader;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.MyProgressLoading;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;
public class ZeroFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    Unbinder unbinder;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.ll_location)
    LinearLayout llLocation;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    private String mParam1;
    private String mParam2;

    private DaRenAdapter mAdapter;
    private RxPermissions rxpermissions;
    private Banner banner;
    private ImageView label1,label2,label3,label4;
    private int page=1;
    private HomeClassifyAdapter classifyAdapter;
    private RecyclerView classifyRecyclerView;
    private ArrayList<LabelClassifyBean.BodyBean> classifyLabel;
//    我的关注
    private boolean isOpen=false;
    private MyProgressLoading loadingDialog;
    private NearByImageAdapter mHeadAdapter;
    private TextView tv_money,tv_order_income,tv_tuiguang_income,tv_salesman;
    private RecyclerView recyclerViewImageHeaders;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private String city;
    private mBroadcastReceiver receiver;

    public ZeroFragment() {
    }

    public static ZeroFragment newInstance(String param1, String param2) {
        ZeroFragment fragment = new ZeroFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog=new MyProgressLoading(getActivity(),R.style.DialogStyle);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zero, container, false);
        unbinder = ButterKnife.bind(this, view);
        IntentFilter intentFilter=new IntentFilter("com.dlwx.location");
        receiver=new mBroadcastReceiver();
        getActivity().registerReceiver(receiver,intentFilter);
        initView();
        loadingDialog.show();
         city= (String) SPUtils.get(getActivity(),Constant.CITY,"郑州");
        tvAddress.setText(city);
        getBanner(city);
        getLabelDatas();
                getList();
        getAttentionDatas();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mHeadAdapter!=null){

            mHeadAdapter.clear();
        }
        if(classifyAdapter!=null){
            classifyAdapter.clear();
        }
//        page=1;
//        mAdapter.clear();
//        lRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
//        getList();
//        getAttentionDatas();
        boolean isForeground = (boolean) SPUtils.get(getActivity(), Constant.isForeground, false);
//        LogUtils.i("isForeground:"+isForeground);
//        if(isForeground){
//            getList();
//            getAttentionDatas();
//        }
        getMyAccount();
    }

    private List<DaRenBean.BodyBean> attentionDatas=new ArrayList<>();
    private List<DaRenBean.BodyBean> myDatas=new ArrayList<>();
    private void getAttentionDatas() {
        String longi = (String) SPUtils.get(getActivity(), Constant.LOCATION_LON, "116.395645");
        String lat = (String) SPUtils.get(getActivity(), Constant.LOCATION_LAT, "39.929985");
        OkgoUtil.post(Http.MyAttention,getActivity())
                .params("long",longi)
                .params("lat",lat)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DaRenBean parentBean=DealResult.getInstance().dealDatas(getContext(),response,new TypeToken<DaRenBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code==Constant.SUCESS){
                            List<DaRenBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){return;}
                            myDatas.clear();
                            attentionDatas.clear();
                            myDatas.addAll(beans.subList(0,1));
                            attentionDatas.addAll(beans.subList(1,beans.size())) ;
                            DaRenBean.BodyBean bean=new DaRenBean.BodyBean();
                            bean.setType(2);
                            attentionDatas.add(bean);
                            if(mAdapter.getDataList().containsAll(myDatas)){
                                mAdapter.getDataList().removeAll(myDatas);
                            }
                            if(mAdapter.getDataList().containsAll(attentionDatas)){
                                mAdapter.getDataList().removeAll(attentionDatas);
                            }
                            if(isOpen){
                                mAdapter.addAllStart(attentionDatas);
                                mAdapter.addAllStart(myDatas);
                            }else{
                                mAdapter.addAllStart(myDatas);
                            }
                        }else {
                            if(code==Constant.EMPTY){
                                recyclerView.setNoMore(true);
                            }else{
                                ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(getActivity(),getString(R.string.bad_net));
                    }
                });
    }

    private void getList() {
        String longi = (String) SPUtils.get(getActivity(), Constant.LOCATION_LON, "116.395645");
        String lat = (String) SPUtils.get(getActivity(), Constant.LOCATION_LAT, "39.929985");
        OkgoUtil.post(Http.SYS_RECOME_LIST,getActivity())
                .params("pageno",page)
                .params("long",longi)
                .params("lat",lat)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        DaRenBean parentBean=DealResult.getInstance().dealDatas(getContext(),response,new TypeToken<DaRenBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code==Constant.SUCESS){
                            List<DaRenBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){return;}
                            mAdapter.addAll(beans);
                             if(ZeroFragment.this.isAdded()){
                                 if(recyclerView==null){return;}
                                recyclerView.refreshComplete(10);
                            }

                        }else {
                            if(ZeroFragment.this.isAdded()){
                            if(code==Constant.EMPTY){

                                    if(recyclerView==null){return;}
                                    recyclerView.setNoMore(true);
                                }
                            }else{
                                ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                            }
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (ZeroFragment.this.isAdded()) {
                            loadingDialog.dismiss();
                            if (recyclerView != null) {
                                recyclerView.refreshComplete(mAdapter.getItemCount());
                            }
//                        mLRecyclerViewAdapter.notifyDataSetChanged();
                            ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        }
                    }
                });
    }

    private void getMyAccount() {
        OkgoUtil.post(Http.MyAccount,getActivity())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        NearByInfoBean parentBean=DealResult.getInstance().dealDatas(getContext(),response
                                ,new TypeToken<NearByInfoBean>() {}.getType());
                        if(parentBean==null){return;}
                        if(!ZeroFragment.this.isAdded()){
                            return;
                        }
                        if(parentBean.getCode()==Constant.SUCESS){
                            setNearByDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if(ZeroFragment.this.isAdded()){

                            ToastUtils.toastForShort(getActivity(),getString(R.string.bad_net));
                        }
                    }
                });
    }

    private void setNearByDatas(NearByInfoBean.BodyBean body) {
        List<NearByInfoBean.BodyBean.HeadurlBean> images = body.getHeadurl();
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewImageHeaders.setLayoutManager(manager);
        mHeadAdapter=new NearByImageAdapter(getActivity(),images);
        recyclerViewImageHeaders.setHasFixedSize(true);
        recyclerViewImageHeaders.setAdapter(mHeadAdapter);
        SPUtils.put(getActivity(),Constant.UserBalance, body.getAccountTotal());
        SPUtils.put(getActivity(),Constant.ORDER_INCOME, body.getWagesTotal());
        SPUtils.put(getActivity(),Constant.TUIGUANG_INCOME, body.getExtendTotal());
        if(isVisible()){
            tv_money.setText(getString(R.string.rmb) + body.getAccountTotal());
            tv_order_income.setText(getString(R.string.rmb) + body.getWagesTotal());
            tv_tuiguang_income.setText(getString(R.string.rmb) + body.getExtendTotal());
        }
        tv_salesman.setText(body.getTextcontent().replaceAll("#","\n"));
        mHeadAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
                Intent i=new Intent(getActivity(), NearByActivity.class);
//                i.putExtra("distance",1);
                startActivity(i);
            }
        });
    }
    private void getBanner(String city) {
        OkgoUtil.get(Http.BANNER_ZERO,getActivity())
                .params("city",city)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BannerBean parentBean=DealResult.getInstance().dealDatas(getContext(),response,new TypeToken<BannerBean>() {}.getType());
                        if(parentBean==null){return;}
                        if(!ZeroFragment.this.isAdded()){return;}
                        if(parentBean.getCode()==Constant.SUCESS||parentBean.getCode()==Constant.EMPTY){
                            List<BannerBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){
                                return;
                            }
                            initBanner(beans);
                        }else {
                            ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if(!ZeroFragment.this.isAdded()){return;}
                        ToastUtils.toastForShort(getActivity(),getString(R.string.bad_net));
                    }
                });
    }
    private void getLabelDatas() {
        OkgoUtil.post(Http.ALL_LABEL,getActivity())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LabelClassifyBean parentBean= DealResult.getInstance().dealDatas(getActivity(),response,new TypeToken<LabelClassifyBean>() {}.getType());
                        if(parentBean==null){return;}
                        if(!ZeroFragment.this.isAdded()){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            List<LabelClassifyBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){return;}
                            classifyLabel.addAll(beans);
                            classifyAdapter.notifyDataSetChanged();
                        }else {
                            if(code==Constant.EMPTY){
                                ToastUtils.toastForShort(getActivity(),getString(R.string.empty_datas));
                            }else{
                                ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if(!ZeroFragment.this.isAdded()){return;}
                        ToastUtils.toastForShort(getActivity(),getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        rxpermissions=new RxPermissions(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DaRenAdapter(getActivity());
        mAdapter.setDataList(new ArrayList<DaRenBean.BodyBean>());
         lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshList();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getList();
            }
        });
        addHeader(lRecyclerViewAdapter);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(position==0){
//                    int listId = mAdapter.getDataList().get(position).getListid();
//                    int userid= (int) SPUtils.get(getActivity(),Constant.USER_ID,0);
//                    if(listId==userid){
//                        startActivity(new Intent(getActivity(), OrderReceActivity.class));
//                    }else{
//                        Intent i = new Intent(getActivity(), AttentionUserClassifyActivity.class);
//                        i.putExtra("listId", mAdapter.getDataList().get(position).getListid() + "");
//                        startActivity(i);
//                    }
                }else {
                    Intent i = new Intent(getActivity(), AttentionUserClassifyActivity.class);
                    i.putExtra("listId", mAdapter.getDataList().get(position).getListid() + "");
                    startActivity(i);
                }
//                }
            }
        });
        recyclerView.setLScrollListener(new LRecyclerView.LScrollListener() {
            @Override
            public void onScrollUp() {}
            @Override
            public void onScrollDown() {}
            @Override
            public void onScrolled(int distanceX, int distanceY) {
                int height = DisplayUtil.dip2px(getActivity(), 200);
//                ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(llLocation, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
                if (distanceY > height) {
                    llLocation.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.location));
                } else {
                    llLocation.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.location_transparent));
                }
            }

            @Override
            public void onScrollStateChanged(int state) {
            }
        });
    }

    private void refreshList() {
        if(mHeadAdapter!=null){

            mHeadAdapter.clear();
        }
        if(classifyAdapter!=null){
            classifyAdapter.clear();
        }
        page=1;
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
        getList();
        getAttentionDatas();
        getBanner(city);
        getLabelDatas();
        getMyAccount();
    }

    private void addHeader(final LRecyclerViewAdapter lRecyclerViewAdapter) {
        CommonHeader header = new CommonHeader(getActivity(), R.layout.header_zero_fragment);
         banner = header.findViewById(R.id.banner);
        EditText etSearch = header.findViewById(R.id.et_search);
         recyclerViewImageHeaders = header.findViewById(R.id.recyclerView_imageheader);
        TextView tvLabelAll = header.findViewById(R.id.tv_label_all);
         tv_money = header.findViewById(R.id.tv_money);
         tv_order_income = header.findViewById(R.id.tv_order_income);
         tv_tuiguang_income = header.findViewById(R.id.tv_tuiguang_income);
        tv_salesman = header.findViewById(R.id.tv_salesman);
        final TextView tvAttention = header.findViewById(R.id.tv_attention);
         label1 = header.findViewById(R.id.image_label_one);
         label2 = header.findViewById(R.id.image_label_two);
         label3 = header.findViewById(R.id.image_label_three);
         label4 = header.findViewById(R.id.image_label_four);
        LinearLayout llNearby = header.findViewById(R.id.ll_nearby);
        classifyRecyclerView=header.findViewById(R.id.classify_recyclerView);
        classifyLabel=new ArrayList<LabelClassifyBean.BodyBean>();
        classifyAdapter = new HomeClassifyAdapter(getActivity(),classifyLabel);
//        classifyAdapter.setDataList(new ArrayList<LabelClassifyBean.BodyBean>());
        final GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        manager.setAutoMeasureEnabled(true);
        manager.setOrientation(GridLayoutManager.HORIZONTAL);
        classifyRecyclerView.setLayoutManager(manager);
        //divider
        GridItemDecoration divider = new GridItemDecoration.Builder(getActivity())
//                .setHorizontal(R.dimen.size_4)
                .setVertical(R.dimen.size_20)
                .setColorResource(R.color.white)
                .build();
//        classifyRecyclerView.setHasFixedSize(true);
//        recyclerView.addItemDecoration(divider);
        classifyRecyclerView.setAdapter(classifyAdapter);
        classifyAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener<LabelClassifyBean.BodyBean>() {
            @Override
            public void onItemClick(View view, LabelClassifyBean.BodyBean data) {
                Intent i=new Intent(getActivity(), ClassifyListActivity.class);
                i.putExtra("labeid",data.getLabelclassid()+"");
                i.putExtra("labename",data.getClassname());
                startActivity(i);
            }
        });
            tvAttention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<DaRenBean.BodyBean> lists= mAdapter.getDataList();
                    Drawable rightDrawable = ContextCompat.getDrawable(getActivity(), R.mipmap.arrow_right);
                    Drawable downDrawable = ContextCompat.getDrawable(getActivity(), R.mipmap.arrow_down);
                    rightDrawable.setBounds(0,0,rightDrawable.getMinimumWidth(),rightDrawable.getMinimumHeight());
                    downDrawable.setBounds(0,0,downDrawable.getMinimumWidth(),downDrawable.getMinimumHeight());
                    if(isOpen){
                        tvAttention.setCompoundDrawables(null,null,rightDrawable,null);
                        isOpen=false;
                        lists.removeAll(attentionDatas);
                        mAdapter.notifyDataSetChanged();
                    }else{
                        tvAttention.setCompoundDrawables(null,null,downDrawable,null);
                        isOpen=true;
                        lists.addAll(1,attentionDatas);
                        mAdapter.notifyDataSetChanged();
                    }
//                    ToastUtils.toastForShort(getActivity(),"条目数量："+mAdapter.getItemCount());
                }
            });

        llNearby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getActivity(), NearByActivity.class);
//                i.putExtra("distance",1);
                startActivity(i);
            }
        });

        etSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });

        tvLabelAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AllClassifyActivity.class));
            }
        });
        tv_salesman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SalesmanActivity.class));
            }
        });

        lRecyclerViewAdapter.addHeaderView(header);
    }
   /*     设置banner*/
    private void initBanner(List<BannerBean.BodyBean> datas){
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        List<String> images = new ArrayList<>();
        for(BannerBean.BodyBean bean:datas){
            images.add(bean.getAd_img());
        }
        banner.setImages(images);
        banner.start();
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(receiver);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.CITY_RESULT) {
            if (data == null) {
                return;
            }
            city=data.getStringExtra("city");
            getBanner(city);
            tvAddress.setText(city);
        }
    }

    @OnClick({R.id.tv_address, R.id.et_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_address:
                rxpermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if(aBoolean){
//                                    startActivityForResult(new Intent(getActivity(), LocationActivity.class), 0);
                                    startActivityForResult(new Intent(getActivity(), CityListActivity.class), 0);
                                }
                            }
                        });
                break;
            case R.id.et_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }
private boolean isFirst=true;
    public  class mBroadcastReceiver extends BroadcastReceiver {
        public mBroadcastReceiver() {
        }
        //接收到广播后自动调用该方法
        @Override
        public void onReceive(Context context, Intent intent) {
            //写入接收广播后的操作
            if(TextUtils.equals(intent.getAction(),"com.dlwx.location")){
                BDLocation location= intent.getParcelableExtra("location");
                if(location!=null){
                    if(isFirst){
                    city= location.getCity();
                    if(city!=null){
                        if(city.endsWith("市")){
                            city= city.substring(0,city.length()-1);
                        }
                            tvAddress.setText(city);
                            getBanner(city);
                            isFirst=false;
                        }
                    }
                }
            }
        }
    }
}
